//
//  SpotSelectPayView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/2.
//

import UIKit

class SpotSelectPayView: UIView {
    var orderInfoModel:OrderListModel!
    var selectBtn:UIButton!

    typealias SpotSelectPayConfirmBlock = () -> Void
    var spotSelectPayConfirmBlock : SpotSelectPayConfirmBlock!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.cencelBtn)
        self.mainView.addSubview(self.titleLab)
        self.mainView.addSubview(self.confirmBtn)

        self.mainView.addSubview(self.visitBtn)
        self.mainView.addSubview(self.stationBtn)
        self.mainView.addSubview(self.airportBtn)
        self.mainView.addSubview(self.hintLab)
        
        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0);
            make.right.equalTo(self.snp.right).offset(0);
            make.bottom.equalTo(self.snp.bottom).offset(0);
            make.height.equalTo(228 + Constants.xSafeBottomHeight);
        }
        self.cencelBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40);
            make.width.equalTo(40);
            make.top.equalTo(self.mainView.snp.top).offset(15);
            make.right.equalTo(self.snp.right).offset(-15);
        }
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(15);
            make.top.equalTo(self.mainView.snp.top).offset(15);
        }
        self.confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(60);
            make.right.equalTo(self.mainView.snp.right).offset(-60);
            make.bottom.equalTo(self.mainView.snp.bottom).offset(-(Constants.xSafeBottomHeight == 0 ? 8 : Constants.xSafeBottomHeight));
            make.height.equalTo(40);
        }
        let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
        var BtnHeightValue:CGFloat = 40
        if (str == LanguageHeader.BurmaLanguage) {
            BtnHeightValue = 60;
        }
        self.visitBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.left.equalTo(self.mainView.snp.left).offset(20);
            make.height.equalTo(BtnHeightValue);
            make.width.equalTo((Constants.screenWidth - 80)/3);
        }
        self.stationBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.centerX.equalTo(self.mainView.snp.centerX);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.airportBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.right.equalTo(self.mainView.snp.right).offset(-20);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.hintLab.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.airportBtn.snp.centerX);
            make.top.equalTo(self.airportBtn.snp.bottom).offset(10);
            make.right.equalTo(self.mainView.snp.right).offset(-20);
        }
        self.setValueUI()
    }
    
    func setValueUI() {
        titleLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_paymentMethod)
        hintLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_collectionDelivery_select)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        visitBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_paidShipper), for: .normal)
        stationBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_topay), for: .normal)
        airportBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_deduction), for: .normal)

        let label : UILabel = airportBtn.titleLabel!
        label.changeLineSpaceForLabel(space: 5.0)
        airportBtn.titleLabel?.textAlignment = .center
        

    }
    func setOrderInfoModel(orderInfoModel:OrderListModel) {
        self.orderInfoModel = orderInfoModel
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        if orderInfoModel.payType == 0 {
            selectBtn = visitBtn
        } else if orderInfoModel.payType == 1 {
            selectBtn = stationBtn
        } else if orderInfoModel.payType == 2 {
            selectBtn = airportBtn
        }
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
        if (orderInfoModel.isCollection != 0) {
            airportBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
            hintLab.isHidden = true
        } else {
            airportBtn.setTitleColor(UIColor.init(hexString: "#B6B6B6"), for: .normal)
        }
    }
    
    @objc func clickCancelBtn()  {
        self.removeFromSuperview()
    }
    
    @objc func clickConfirmBtn()  {
        if selectBtn == nil {
            Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_payType_select))
            return
        }
        if selectBtn.tag == 120 {
            orderInfoModel.payType = 0
        } else if selectBtn.tag == 121 {
            orderInfoModel.payType = 1
        } else if selectBtn.tag == 122 {
            orderInfoModel.payType = 2
        }
        if (spotSelectPayConfirmBlock != nil) {
            spotSelectPayConfirmBlock()
        }
        self.removeFromSuperview()
    }
    
    @objc func clickSelectDeliver(sender:UIButton)  {
        if selectBtn == sender {
            return
        }
        if sender.tag == 120 {
            orderInfoModel.payType = 0
        } else if sender.tag == 121 {
            orderInfoModel.payType = 1
        } else {
            if (orderInfoModel.isCollection != 0) {
                hintLab.isHidden = false
                return
            }
            orderInfoModel.payType = 2
        }
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        selectBtn = sender
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
    }
    
    lazy var mainView: UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView
    }()
    
    lazy var cencelBtn: UIButton = {
        let cencelBtn = UIButton.init(type: .custom)
        cencelBtn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        cencelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cencelBtn
    }()
    
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.font = UIFont.boldSystemFont(ofSize: 17)
        titleLab.textColor  = UIColor.init(hexString: "#4C4C4C")
        return titleLab
    }()
    
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.addTarget(self, action: #selector(clickConfirmBtn), for: .touchUpInside)
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.layer.masksToBounds = true
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return confirmBtn
    }()
    
    lazy var visitBtn: UIButton = {
        let visitBtn = UIButton.init(type: .custom)
        visitBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        visitBtn.backgroundColor = UIColor.white
        visitBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        visitBtn.layer.masksToBounds = true
        visitBtn.layer.cornerRadius = 20
        visitBtn.tag = 120
        visitBtn.layer.borderWidth = 0.5
        visitBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        visitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        visitBtn.titleLabel?.textAlignment = .center
        visitBtn.titleLabel?.numberOfLines = 0;
        return visitBtn
    }()
    lazy var stationBtn: UIButton = {
        let stationBtn = UIButton.init(type: .custom)
        stationBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        stationBtn.backgroundColor = UIColor.white
        stationBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        stationBtn.layer.masksToBounds = true
        stationBtn.layer.cornerRadius = 20
        stationBtn.tag = 121
        stationBtn.layer.borderWidth = 0.5
        stationBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        stationBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        stationBtn.titleLabel?.textAlignment = .center
        stationBtn.titleLabel?.numberOfLines = 0;
        return stationBtn
    }()
    lazy var airportBtn: UIButton = {
        let airportBtn = UIButton.init(type: .custom)
        airportBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        airportBtn.backgroundColor = UIColor.white
        airportBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        airportBtn.layer.masksToBounds = true
        airportBtn.layer.cornerRadius = 20
        airportBtn.tag = 122
        airportBtn.layer.borderWidth = 0.5
        airportBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        airportBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        airportBtn.titleLabel?.textAlignment = .center
        airportBtn.titleLabel?.numberOfLines = 0;
        return airportBtn
    }()
    
    lazy var hintLab: UILabel = {
        let hintLab = UILabel()
        hintLab.font = UIFont.systemFont(ofSize: 10)
        hintLab.textColor = UIColor.init(hexString: "#FA1212")
        hintLab.numberOfLines = 0
        hintLab.isHidden = true
        return hintLab
    }()
}
